<?php
namespace Home\Controller;
use Think\Controller;
class ComController extends Controller {
	//$this->error = '';
	public function login(){
		if(IS_POST){//post到本页
			$this->do_login();
		}
		$this->display();
	}
	public function do_login() {
		$userMod = M('user');
		$where['username'] = I('username', '');
		$where['state'] = 1;
		$password = md5(I('post.usercode', ''));
		if(empty($where['username'])){
			//处理错误
			$this->error = '用户名不能为空';
			/**
			 * @todo 错误处理 比如
			 */
			$this->error($this->error);
		}
		$userinfo = $userMod->where($where)->find();
		if(!empty($userinfo) || $userinfo === false){
			
			if($password === $userinfo['usercode']) {
				unset($userinfo['usercode']);
				session('user_info',$userinfo);//用户登录session创建
				/**
				 * @todo 用户判断等级跳转不同页面
				 */
				$this->success('登录成功', '/User/index');
				exit;
			} else {
				/**
				 * @todo 密码错误处理
				 */
				$this->error = '密码错误';
			}
		} else {
			/**
			 * @todo 用户不存在处理
			 */
			$this->error = '用户不存在';
			
		}
		$this->error($this->error);
	}
}